package com.matt.service.impl;

import com.dotflat.cc.po.ChildrenImportPO;
import com.dotflat.cc.po.ChildrenPO;
import com.dotflat.cc.po.LeadsPO;
import com.dotflat.cc.po.ParentUserPO;
import com.dotflat.util.AssertUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.matt.commons.dto.sso.AreaDTO;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.utils.MapConvertUtil;
import com.matt.commons.utils.RemoteCallUtil;
import com.matt.domain.*;
import com.matt.domain.course.ChildrenCourse;
import com.matt.domain.course.ChildrenCourseChangeRecord;
import com.matt.domain.mp.ParentUser;
import com.matt.domain.record.CheckRecord;
import com.matt.domain.rel.RelClassesChildren;
import com.matt.domain.rel.RelUserChildren;
import com.matt.domain.vo.ChildrenContractInfoRecordVo;
import com.matt.foundation.DO.*;
import com.matt.foundation.constant.*;
import com.matt.foundation.dto.crm.ProductDTO;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.query.ChildrenNurseryQuery;
import com.matt.foundation.query.ChildrenQuery;
import com.matt.foundation.utils.CopyUtils;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.utils.JsonValidateUtil;
import com.matt.foundation.utils.annotation.OptsTrace;
import com.matt.foundation.vo.*;
import com.matt.foundation.vo.children.BabyNClassInfoVO;
import com.matt.foundation.vo.children.WebChildrenVo;
import com.matt.repository.*;
import com.matt.repository.base.*;
import com.matt.repository.course.ChildrenCourseChangeRecordRepository;
import com.matt.repository.course.ChildrenCourseRepository;
import com.matt.repository.record.CheckRecordRepository;
import com.matt.repository.record.ChildrenNurseryInfoRecordRepository;
import com.matt.repository.rel.RelClassesChildrenRepository;
import com.matt.repository.rel.RelUserChildrenRepository;
import com.matt.service.*;
import com.matt.service.remoteCall.SsoCallService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.matt.commons.model.ErrorCode.DATE_FORMAT_ERROR;
import static com.matt.foundation.model.ErrorCode.*;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/13
 * *******************************
 */
@Service
@Slf4j
public class RelChildrenServiceMemorandumImpl implements RelChildrenMemorandumService {

    @Autowired
    private RelChildrenMemorandumRepository relChildrenMemorandumRepository;

    @Override
    public Page<RelChildrenMemorandum> findRelChildrenMemorandumPage(String id,String classesId,String name,Integer pageNum,Integer pageSize) {

        // 使用 Specification 构建动态查询条件
        Specification<RelChildrenMemorandum> specification = (root, query, criteriaBuilder) -> {
            // 创建一个空的 Predicate 列表，用于存储查询条件
            List<Predicate> predicates = new ArrayList<>();

            // 判断 classesId 是否为空，不为空则添加到查询条件中
            if (id != null && !id.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("id"), id));
            }

            // 判断 classesId 是否为空，不为空则添加到查询条件中
            if (classesId != null && !classesId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("classesId"), classesId));
            }

            // 判断 name 是否为空，不为空则添加到查询条件中
            if (name != null && !name.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }

            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        Sort sort = Sort.by(Sort.Order.desc("createTime"));

        Page<RelChildrenMemorandum> all = relChildrenMemorandumRepository.findAll(specification, PageRequest.of(pageNum, pageSize,sort));
        return all;
    }


    @Override
    public void addRelChildrenMemorandum(RelChildrenMemorandum relChildrenMemorandum) {
        relChildrenMemorandumRepository.save(relChildrenMemorandum);
    }

    @Override
    public void delRelChildrenMemorandum(String id) {
        relChildrenMemorandumRepository.deleteById(id);
    }
}
